<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\WordResource;
use App\Models\Book;
use App\Models\Word;
use Illuminate\Http\Request;
use Jiannei\Response\Laravel\Support\Facades\Response;

class WordController extends Controller
{
    public function index(Request $request)
    {
        $bookId = $request->book_id ?: Book::query()->scopes(['enable', 'sort'])->first()->id;
        $words = Word::query()->with(['trans', 'phrases', 'sentences'])
            ->where('book_id', $bookId)
            ->scopes(['enable', 'sort'])
            ->paginate(getPerPage());

        return Response::success(WordResource::collection($words));
    }

    public function show(Word $word)
    {
        return Response::success((new WordResource($word)));
    }

}
